package com.android.king.vo;

import android.content.Context;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * xum
 */
public class MyFilePicker {

    public static final String TAG = MyFilePicker.class.getSimpleName();
    public static final int REQUEST_CODE_TAKE = 1001;
    public static final int REQUEST_CODE_CROP = 1002;
    public static final int REQUEST_CODE_PREVIEW = 1003;
    public static final int RESULT_CODE_ITEMS = 1004;
    public static final int RESULT_CODE_BACK = 1005;
    public static final String ACTIVITY_KEY_RESULT_PATHLIST = "file_path_list";

    public static final String EXTRA_RESULT_ITEMS = "extra_result_items";
    public static final String EXTRA_SELECTED_FILE_POSITION = "selected_file_position";
    public static final String EXTRA_FILE_ITEMS = "extra_file_items";
    public static final String EXTRA_FROM_ITEMS = "extra_from_items";

    private boolean multiMode = true;    //图片选择模式
    private int selectLimit = 9;         //最大选择图片数量
    private File cropCacheFolder;
    //private File takeImageFile;


    private ArrayList<MyFileItem> mSelectedImages = new ArrayList<>();   //选中的图片集合
    private List<MyFileFolder> myFileFolders;      //所有的文件的文件夹
    private int mCurrentFileFolderPosition = 0;  //当前选中的文件夹位置 0表示所有图片
    private List<OnFileSelectedListener> mFileSelectedListeners;          // 文件选中的监听回调

    private static MyFilePicker mInstance;

    private MyFilePicker() {
    }

    public static MyFilePicker getInstance() {
        if (mInstance == null) {
            synchronized (MyFilePicker.class) {
                if (mInstance == null) {
                    mInstance = new MyFilePicker();
                }
            }
        }
        return mInstance;
    }
    public boolean isMultiMode() {
        return multiMode;
    }

    public void setMultiMode(boolean multiMode) {
        this.multiMode = multiMode;
    }

    public int getSelectLimit() {
        return selectLimit;
    }

    public void setSelectLimit(int selectLimit) {
        this.selectLimit = selectLimit;
    }

    public File getCropCacheFolder(Context context) {
        if (cropCacheFolder == null) {
            cropCacheFolder = new File(context.getCacheDir() + "/FilePicker/cropTemp/");
        }
        return cropCacheFolder;
    }

    public void setCropCacheFolder(File cropCacheFolder) {
        this.cropCacheFolder = cropCacheFolder;
    }



    public List<MyFileFolder> getImageFolders() {
        return myFileFolders;
    }

    public void setImageFolders(List<MyFileFolder> myFileFolders) {
        myFileFolders = myFileFolders;
    }

    public int getCurrentImageFolderPosition() {
        return mCurrentFileFolderPosition;
    }

    public void setCurrentImageFolderPosition(int mCurrentSelectedFileSetPosition) {
        mCurrentFileFolderPosition = mCurrentSelectedFileSetPosition;
    }

    public ArrayList<MyFileItem> getCurrentFileFolderItems() {
        return myFileFolders.get(mCurrentFileFolderPosition).files;
    }

    public boolean isSelect(MyFileItem item) {
        return mSelectedImages.contains(item);
    }

    public int getSelectImageCount() {
        if (mSelectedImages == null) {
            return 0;
        }
        return mSelectedImages.size();
    }

    public ArrayList<MyFileItem> getSelectedImages() {
        return mSelectedImages;
    }

    public void clearSelectedImages() {
        if (mSelectedImages != null) mSelectedImages.clear();
    }

    public void clear() {
        if (mFileSelectedListeners != null) {
            mFileSelectedListeners.clear();
            mFileSelectedListeners = null;
        }
        if (myFileFolders != null) {
            myFileFolders.clear();
            myFileFolders = null;
        }
        if (mSelectedImages != null) {
            mSelectedImages.clear();
        }
        mCurrentFileFolderPosition = 0;
    }
    /**
     * 图片选中的监听
     */
    public interface OnFileSelectedListener {
        void onFileSelected(int position, MyFileItem item, boolean isAdd);
    }



    public void addOnFileSelectedListener(OnFileSelectedListener l) {
        if (mFileSelectedListeners == null) mFileSelectedListeners = new ArrayList<>();
        mFileSelectedListeners.add(l);
    }

    public void removeOnFileSelectedListener(OnFileSelectedListener l) {
        if (mFileSelectedListeners == null) return;
        mFileSelectedListeners.remove(l);
    }

    public void addSelectedImageItem(int position, MyFileItem item, boolean isAdd) {
        if (isAdd) mSelectedImages.add(item);
        else mSelectedImages.remove(item);
        notifyImageSelectedChanged(position, item, isAdd);
    }

    public void setSelectedImages(ArrayList<MyFileItem> selectedImages) {
        if (selectedImages == null) {
            return;
        }
        this.mSelectedImages = selectedImages;
    }

    private void notifyImageSelectedChanged(int position, MyFileItem item, boolean isAdd) {
        if (mFileSelectedListeners == null) return;
        for (OnFileSelectedListener l : mFileSelectedListeners) {
            l.onFileSelected(position, item, isAdd);
        }
    }
}
